<?php

class ZoneController extends Exts_Controller_GenericSecureController {

	//protected $_authenticate = true;
	protected $_model = 'Application_Model_LocZones';
	protected $_newCreateFormFactory = 'Application_Form_Zone';
	protected $_newUpdateFormFactory = 'Application_Form_Zone';
	protected $_defaultUrl = "/zone/list/";
	protected $_idReqKey = "id";

	public function createAction() {
		parent::createAction();
	}

	public function updateAction() {
		parent::updateAction();
	}

	public function listAction() {
		parent::listAction();
	}

	public function getallAction() {
		$this->_helper->layout->disableLayout();
		$parent = 0;
		if ($this->_hasParam('parent')) {
			$parent = (int) $this->_getParam('parent');
		}
		$model = new Application_Model_LocZones();

		$current = $parent;
		$combos = array();
		$invLvl = 0;

		$aZone = $model->findById($current);

		if ($aZone->parent_id != 0) {
			if (count($childs = $aZone->getChilds()->toSelectArray()) > 0) {
				$combos[$invLvl] = new Zend_Form_Element_Select("combo" . $aZone->zone_id);
				$combos[$invLvl]->setMultiOptions($childs);
				$combos[$invLvl]->setAttrib("onchange", "loadChildsAjaxCall(this, loadChilds);");

				$invLvl++;
			}
			if (($model->getParent($aZone->zone_id)->parent_id != 0) && count($bros = $aZone->getBrothers()->toSelectArray()) > 0) {
				$bros[-1] = "Seleccione";
				$combos[$invLvl] = new Zend_Form_Element_Select("combo" . $aZone->parent_id);
				$combos[$invLvl]->setMultiOptions($bros);
				$combos[$invLvl]->setValue($aZone->zone_id);
				$combos[$invLvl]->setAttrib("onchange", "loadChildsAjaxCall(this, loadChilds);");
				$invLvl++;
			}
		}
		$current = $aZone->zone_id;

		while ($aZone = $model->getParent($current)) {
			$prnt = $model->getParent($aZone->zone_id);
			if ($prnt && $prnt->parent_id != 0) {
				if ($aZone->parent_id != 0 && count($options = $aZone->getBrothers()->toSelectArray()) > 0) {
					$options[-1] = "Seleccione";
					$combos[$invLvl] = new Zend_Form_Element_Select("combo" . $aZone->parent_id);
					$combos[$invLvl]->setMultiOptions($options);
					$combos[$invLvl]->setValue($aZone->zone_id);
					$combos[$invLvl]->setAttrib("onchange", "loadChildsAjaxCall(this, loadChilds);");
					$invLvl++;
				}
			}
			$current = $aZone->zone_id;
		}
		$this->view->combos = $combos;
	}

}